package com.pptree.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.pptree.dao.GenreDAO;
import com.pptree.util.BaseDAO;
import com.pptree.vo.Genre;

public class GenreDAOImpl extends BaseDAO implements GenreDAO {

	@Override
		public int addGenre(Genre genre) {
		try {
			String sql = "insert into category(name)values(?)";
			return this.executeUpdate(sql, genre.getName());
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public int delGenre(int id) {
		String sql = "delete from category where id=?";
		return this.executeUpdate(sql,id);
	}

	@Override
	public int updGenre(Genre genre) {
		try {
			String sql = "update category set name=? where id=?";
			return this.executeUpdate(sql,genre.getName(),genre.getId());
		} catch (Exception e) {
			throw e;
		}
	}

	@Override
	public Genre findGenreById(int id) {
		Genre genre  = null;
		try {
			String sql = "select * from category where id=?";
			rs = executeQuery(sql, id);
			
			try {
				if(rs.next()){
					genre = new Genre();
					genre.setId(rs.getInt("id"));
					genre.setName(rs.getString("name"));
					genre.setCreateDate(rs.getTimestamp("createdate"));
				}
			} 
			catch (SQLException e) {
				e.printStackTrace();
			}
			finally{
				this.closeAll(rs, pstmt, conn);
			}
		} catch (Exception e) {
			throw e;
		}
		
		return genre;
		
		
	}

	@Override
	public List<Genre> findAllGenre() {
		List<Genre> genreList = new ArrayList<Genre>();
		
		
		try {
			String sql = "select * from category";
			rs = executeQuery(sql, new Object[] {});
			
			try {
				
				while(rs.next()){
					Genre genre = new Genre();
					genre.setId(rs.getInt("id"));
					genre.setName(rs.getString("name"));
					genre.setCreateDate(rs.getTimestamp("createdate"));
					
					genreList.add(genre);
				}
			} 
			catch (SQLException e) {
				e.printStackTrace();
			}
			finally{
				this.closeAll(rs, pstmt, conn);
			}
		} catch (Exception e) {
			throw e;
		}
		
		return genreList;
	}

	@Override
	public Genre findGenreByName(String name) {
		Genre genre  = null;
		
		try {
			String sql = "select * from category where name=?";
			rs = executeQuery(sql, name);
			
			try {
				if(rs.next()){
					genre = new Genre();
					genre.setId(rs.getInt("id"));
					genre.setName(rs.getString("name"));
					genre.setCreateDate(rs.getTimestamp("createdate"));
				}
			} 
			catch (SQLException e) {
				e.printStackTrace();
			}
			finally{
				this.closeAll(rs, pstmt, conn);
			}
		} catch (Exception e) {
			throw e;
		}
		
		return genre;
	}

	@Override
	public boolean updExistsName(String oldName,String newName) {
		String sql = "SELECT  * FROM category WHERE `name` <> ? AND `name`= ?";
		rs = executeQuery(sql, oldName,newName);
		try {
			if(rs.next()){
				return true;
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	

	

}
